package com.heima.comment.service.imp;

import com.heima.comment.service.CommentHotService;
import com.heima.model.comment.pojo.ApComment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author TheCai
 * @date 2021/6/5 23:44
 */
@Service
@Slf4j
public class CommentHotServiceImpl implements CommentHotService {
  @Autowired MongoTemplate mongoTemplate;
  /** 查找热点评论 */
  @Async("taskExecutor") // 异步调用,线程池
  @Override
  public void hotCommentExecutor(ApComment apComment) {
    log.info("异步计算热点文章==================> 开始");
    // 1. 按照文章id   flag=1(热点文章)   点赞降序
    Long entryId = apComment.getEntryId();

    Query query =
        Query.query(Criteria.where("entryId").is(entryId).and("flag").is(1))
            .with(Sort.by(Sort.Direction.DESC, "likes"));

    // 得到热评列表
    List<ApComment> hotApComments = mongoTemplate.find(query, ApComment.class);
    // 2. 如果 热评集合为空  或  数量小于5 直接将当前评论改为热评
    if (hotApComments == null || hotApComments.size() < 5) {
      // 满足条件直接改为热评
      apComment.setFlag((short) 1);
      mongoTemplate.save(apComment);
      return;
    }
    // 3. 获取热评集合中 最后点赞数量最少的热评
    ApComment lastHotApComment = hotApComments.get(hotApComments.size() - 1);

    // 4. 和当前评论点赞数量做对比  谁的点赞数量多 改为热评
    if (apComment.getLikes() > lastHotApComment.getLikes()) {
      apComment.setFlag((short) 1);
      lastHotApComment.setFlag((short) 0);
      mongoTemplate.save(apComment);
      mongoTemplate.save(lastHotApComment);
    }
    log.info("异步计算热点文章==================> 结束");
  }
}
